from django.core.files.storage import Storage
from django.conf import settings
from django.core.files import File
from fdfs_client.client import Fdfs_client


class FdfsStorage(Storage):
    # def __init__(self, client_conf=None, tracker_server=None):
    #     self.client_conf = 'fastdfs/client.conf'
    #     self.tracker_server = 'http://192.168.85.136:8888/'
    #
    # def _open(self, name, mode='rb'):
    #     """
    #     Retrieves the specified file from storage.
    #     """
    #     pass

    # def _save(self, name, content):
    #     f_client = Fdfs_client(settings.FDFS_PATH)
    #     res = f_client.upload_by_buffer(content.read())
    #     # res 返回值
    #     # dict
    #     # {
    #     #     'Group name': group_name,
    #     #     'Remote file_id': remote_file_id,
    #     #     'Status': 'Upload successed.',
    #     #     'Local file name': '',
    #     #     'Uploaded size': upload_size,
    #     #     'Storage IP': storage_ip
    #     # }
    #     if res.get('Status') != 'Upload successed.':
    #         raise Exception('上传失败')
    #     filename = res.get('Remote file_id')
    #     print(filename)
    #     return filename.replace('\\', '/')

    # def exists(self, name):
    #     return False

    def url(self, name):
        return settings.IMAGE_URL + name
